package io.xxx.mario.robot;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.xxx.mario.robot.api.ApplyRobotRequest;
import io.xxx.mario.robot.api.SaveRobotRequest;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 机器人管理
 */
@Tag(name = "Robot", description = "微信机器人管理")
@RestController
@RequestMapping("/robot")
public class RobotController {

    @Resource
    private RobotService robotService;

    @Operation(summary = "登录微信机器人", description = "登录微信机器人需要消耗一个Token")
    @PostMapping("/login")
    public String getLoginQrcode() {
        return robotService.getLoginQrcode();
    }

    @Operation(summary = "保存机器人信息")
    @PostMapping("/save")
    public void save(@RequestBody SaveRobotRequest request) {
        robotService.save(request);
    }

    /**
     * 失效机器人
     */
    @PostMapping("/invalid")
    public void invalid(@RequestBody Robot robot) {
        if (robot.getId() == null) {
            throw new IllegalArgumentException("机器人ID不存在");
        }
        if (robot.getPuppetId() == null) {
            throw new IllegalArgumentException("Token不存在");
        }
    }

    @Operation(summary = "申请机器人", description = "门店老板申请机器人，根据分配规则为其分配一个机器人")
    @PostMapping("/apply")
    public Robot apply(@RequestBody ApplyRobotRequest request) {
        return robotService.applyRobot(request);
    }
}
